package com.example.controller;

import com.example.common.Result;
import com.example.service.PhoneVerificationService;
import com.example.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 手机号验证控制器
 */
@RestController
@RequestMapping("/api/phone")
@CrossOrigin
public class PhoneVerificationController {
    
    @Autowired
    private PhoneVerificationService phoneVerificationService;

    @Autowired
    private SystemConfigService systemConfigService;
    
    /**
     * 发送注册验证码
     */
    @PostMapping("/send-register-code")
    public Result<Void> sendRegisterCode(@RequestBody Map<String, String> request) {
        String phone = request.get("phone");
        if (phone == null || phone.trim().isEmpty()) {
            return Result.error("手机号不能为空");
        }
        return phoneVerificationService.sendRegisterCode(phone.trim());
    }
    
    /**
     * 发送登录验证码
     */
    @PostMapping("/send-login-code")
    public Result<Void> sendLoginCode(@RequestBody Map<String, String> request) {
        String phone = request.get("phone");
        if (phone == null || phone.trim().isEmpty()) {
            return Result.error("手机号不能为空");
        }
        return phoneVerificationService.sendLoginCode(phone.trim());
    }
    
    /**
     * 验证注册验证码
     */
    @PostMapping("/verify-register-code")
    public Result<Void> verifyRegisterCode(@RequestBody Map<String, String> request) {
        String phone = request.get("phone");
        String code = request.get("code");
        
        if (phone == null || phone.trim().isEmpty()) {
            return Result.error("手机号不能为空");
        }
        if (code == null || code.trim().isEmpty()) {
            return Result.error("验证码不能为空");
        }
        
        return phoneVerificationService.verifyRegisterCode(phone.trim(), code.trim());
    }
    
    /**
     * 验证登录验证码
     */
    @PostMapping("/verify-login-code")
    public Result<Void> verifyLoginCode(@RequestBody Map<String, String> request) {
        String phone = request.get("phone");
        String code = request.get("code");
        
        if (phone == null || phone.trim().isEmpty()) {
            return Result.error("手机号不能为空");
        }
        if (code == null || code.trim().isEmpty()) {
            return Result.error("验证码不能为空");
        }
        
        return phoneVerificationService.verifyLoginCode(phone.trim(), code.trim());
    }
    
    /**
     * 检查手机号是否已注册
     */
    @GetMapping("/check-phone/{phone}")
    public Result<Map<String, Boolean>> checkPhone(@PathVariable String phone) {
        boolean isRegistered = phoneVerificationService.isPhoneRegistered(phone);
        return Result.success(Map.of("isRegistered", isRegistered));
    }

    /**
     * 获取手机号功能配置状态（公开API）
     */
    @GetMapping("/config/status")
    public Result<Map<String, Boolean>> getPhoneConfigStatus() {
        Map<String, Boolean> status = Map.of(
            "phoneEnabled", systemConfigService.isPhoneEnabled(),
            "phoneRegisterEnabled", systemConfigService.isPhoneRegisterEnabled(),
            "phoneLoginEnabled", systemConfigService.isPhoneLoginEnabled()
        );
        return Result.success(status);
    }
}
